home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1996 February / EnigmA AMIGA RUN 04 (1996)(G.R. Edizioni)(IT)[!][issue 1996-02][Skylink CD III].iso / earcd / util1 / yk211src.lha / Yak_2.11_Src / WBStartup / LastActiveWindow.c < prev    next >
C/C++ Source or Header  |  1995-10-18  |  5KB  |  254 lines

  1. /*
  2.  * LastActiveWindow.c
  3.  *
  4.  * Routines to make Yak remember last active window on screen already visited
  5.  * Part of Yak.
  6.  *
  7.  * Gael Marziou, 6/94.
  8.  *
  9.  * This code doesn't patch intuition functions.
  10.  * I tried to patch ScreenToFront() and ScreenToBack() so that even clicking on 
  11.  * screens depth gadget would have notified Yak, but unfortunately when changing 
  12.  * screens via depth gadget Intuition doesn't use these functions.
  13.  *
  14.  */
  15. #include <exec/types.h>
  16. #include <intuition/intuition.h>
  17. #include <intuition/intuitionbase.h>
  18. #include <proto/exec.h>
  19. #include <proto/intuition.h>
  20.  
  21. #include "code.h"
  22. #include "yak.h"
  23. #include "Handlers.h"
  24. #include "LastActiveWindow.h"
  25.  
  26.  
  27. #define SCREEN_ARRAY_SIZE 10
  28.  
  29. struct {
  30.     struct Screen *Screen;
  31.     struct Window *Window;
  32. } ScreenArray[SCREEN_ARRAY_SIZE];
  33.  
  34.  
  35. /* Find a screen in our list */
  36.  
  37. UBYTE
  38. FoundScreenInList( struct Screen *Screen)
  39. {
  40.     UBYTE i=0;
  41.  
  42.     /* Try to found a screen that matches ours */
  43.     while ((ScreenArray[i].Screen  != Screen) && 
  44.            (i<SCREEN_ARRAY_SIZE-1))
  45.     {
  46.         i++;
  47.     }
  48.  
  49.     if ((i == SCREEN_ARRAY_SIZE - 1) && 
  50.         (ScreenArray[i].Screen  != Screen))
  51.     {
  52.         i = SCREEN_ARRAY_SIZE;
  53.     }
  54.     return(i);
  55. }
  56.  
  57. /* Try to find dead screens in our list in order to provide some space 
  58.  * If it succeeds, it will return a free index in ScreenArray otherwise 
  59.  * it will return SCREEN_ARRAY_SIZE.
  60.  */
  61.  
  62. UBYTE 
  63. GarbageCollectorScreenArray(void)
  64. {
  65.     struct Screen *scr;
  66.     UBYTE i;
  67.     ULONG lock;
  68.     BOOL  Lost;
  69.     UBYTE result = SCREEN_ARRAY_SIZE;
  70.  
  71.     lock = LockIBase(0);
  72.  
  73.     for (i=0; i<SCREEN_ARRAY_SIZE-1 ; i++)
  74.     {
  75.         Lost = TRUE;
  76.         for (scr = IntuitionBase->FirstScreen; scr && Lost ; scr = scr->NextScreen)
  77.         {
  78.             Lost = (scr != ScreenArray[i].Screen);
  79.         }
  80.         if (Lost)
  81.         {
  82.             /* Remove dead screen */
  83.             ScreenArray[i].Screen = NULL;
  84.             result = i;
  85.         }
  86.     }
  87.     UnlockIBase(lock);
  88.     return(result);
  89. }
  90.  
  91.  
  92. /* Store active window of current screen */
  93.  
  94. void
  95. RememberActiveWindow(struct Window *win)
  96. {
  97.     UBYTE i;
  98.  
  99.     if (win == NULL)
  100.     {
  101.         ULONG lock = LockIBase(0);
  102.         win = IntuitionBase->ActiveWindow;
  103.         UnlockIBase(lock);
  104.     }
  105.  
  106.     if (win)
  107.     {
  108.         i = FoundScreenInList(win->WScreen);
  109.         if (i < SCREEN_ARRAY_SIZE)
  110.         {
  111.             ScreenArray[i].Window = win; 
  112.         }
  113.         else
  114.         {
  115.             /* Our screen hasn't been yet visited so add it to our list */
  116.             i = FoundScreenInList(NULL);
  117.             if (i == SCREEN_ARRAY_SIZE)
  118.             {
  119.                 /* We haven't found a free space, try to free some */
  120.                 i = GarbageCollectorScreenArray();
  121.             }
  122.             if (i < SCREEN_ARRAY_SIZE)
  123.             {
  124.                 /* We have found a free space */
  125.                 ScreenArray[i].Screen = win->WScreen;
  126.                 ScreenArray[i].Window = win; 
  127.             }
  128.         }
  129.     }
  130. }
  131.  
  132.  
  133. /* Returns a pointer on active window of last time we visit this screen 
  134.  * if possible.
  135.  */
  136.  
  137. struct Window 
  138. *LastActiveWindow(struct Screen *CurrentScreen)
  139. {
  140.     struct Window *win;
  141.     UBYTE i;
  142.  
  143.     if (CurrentScreen)
  144.     {
  145.         /* default */
  146.         win = CurrentScreen->FirstWindow;
  147.  
  148.         i = FoundScreenInList(CurrentScreen);    
  149.         if (i < SCREEN_ARRAY_SIZE)
  150.         {
  151.             /* Is our window still alive or still on our screen ? */
  152.             for (win = CurrentScreen->FirstWindow; win ; win = win->NextWindow)
  153.             {
  154.                 if (win == ScreenArray[i].Window)
  155.                     break;
  156.             }
  157.             if (!win) 
  158.                 win = CurrentScreen->FirstWindow;
  159.         }
  160.     }
  161.     else
  162.     {
  163.         win = NULL;
  164.     }
  165.     return(win);
  166. }
  167.  
  168. extern struct Screen *RearmostScreen(void); /* declared in hotkey_actions.c */
  169.  
  170. /*
  171.  * Simulates screen cycling through depth screen gadget
  172.  */
  173. void
  174. ScreenDepthGadget(void)
  175. {
  176.     struct Screen *screen = ScreenUnderMouse();
  177.  
  178.     if (screen == RearmostScreen())
  179.     {
  180.         MyScreenToFront(screen);
  181.     }
  182.     else
  183.     {
  184.         MyScreenToBack(screen);
  185.     }
  186. }
  187.  
  188.  
  189. #ifdef DEBUG_LAW
  190.  
  191. /* Well it has been debugged but who knows, it can help in the future */
  192.  
  193.  
  194. void
  195. main(void)
  196. {
  197.     char c = 0;
  198.     struct Screen *scr;
  199.     ULONG i;
  200.     ULONG lock;
  201.     
  202.     while(c!='q')
  203.     {
  204.         scanf("%c",&c);
  205.         switch (c)
  206.         {
  207.           case 'r':    
  208.             for (i=0; i<SCREEN_ARRAY_SIZE; i++)
  209.             {
  210.                 printf("i = %d\n",i);
  211.                 if (ScreenArray[i].Screen)
  212.                 {
  213.                     printf("ScreenArray[%d].Screen = %d\n",i,ScreenArray[i].Screen);
  214.                     if (ScreenArray[i].Window->Title)
  215.                         printf("ScreenArray[%d].Window->Title = %s\n",i,ScreenArray[i].Window->Title);
  216.                 }
  217.             }
  218.             break;
  219.  
  220.           case 'c':
  221.             lock = LockIBase(0);
  222.             scr = IntuitionBase->FirstScreen;
  223.             UnlockIBase(lock);
  224.             
  225.             RememberActiveWindow();
  226.             ScreenToFront(scr->NextScreen);
  227.             Delay(200);
  228.             RememberActiveWindow();
  229.             break;
  230.  
  231.           case 'g': 
  232.             GarbageCollectorScreenArray();
  233.             break;
  234.  
  235.           case 'a':
  236.             scanf("%d",&i);
  237.             if (LastActiveWindow(ScreenArray[i].Screen))
  238.             {
  239.                 if (LastActiveWindow(ScreenArray[i].Screen)->Title)
  240.                     printf("LastActiveWindow(ScreenArray[%d].Screen)->Title = %s\n",i,LastActiveWindow(ScreenArray[i].Screen)->Title);
  241.             }
  242.             else
  243.             {
  244.                 printf("NULL\n");
  245.             }
  246.             break;
  247.         }
  248.     }
  249.  
  250. }
  251.  
  252. #endif
  253.  
  254.